package person.drf.shop.product.jucTest;

import java.util.concurrent.*;

public class MyThreadPoolDemo {
    public static void main(String[] args) {
        //前四个比较好理解
        //第5个堵塞队列
        //第6个线程工厂，使用默认
        //第7个是拒绝策略，使用默认策略
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                2L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy()  //拒绝策略
        );
        try {
            for (int i = 0; i < 10; i++) {   //模拟9个
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"办理业务");
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
    }






    private static void init() {
        //一池5个工作线程,类似一个银行有5个办理窗口
        //ExecutorService threadPool = Executors.newFixedThreadPool(5);
        //一池一线
        //ExecutorService threadPool = Executors.newSingleThreadExecutor();
        //一池有N个线程，类似一个银行有N个办理窗口
        ExecutorService threadPool = Executors.newCachedThreadPool();
        try {
            //模拟10个顾客过来办理业务，而池子只有5个工作人员
            for (int i = 0; i < 10; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"办理业务");
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
    }
}
